package com.dodo.blog.ui.page;

import com.dodo.blog.RequestCycle;
import com.dodo.blog.WebApplication;
import com.dodo.blog.exception.BackendException;
import com.dodo.blog.exception.ValidationException;
import com.dodo.blog.model.Account;
import com.dodo.blog.ui.ajax.AjaxListenerHelper;
import com.dodo.blog.ui.component.Component;
import com.dodo.blog.ui.component.composite.TwoColumnLayout;
import com.dodo.blog.ui.component.container.Panel;
import com.dodo.blog.ui.component.container.Title;
import com.dodo.blog.ui.message.FeedbackMessage;
import com.dodo.blog.ui.message.FieldMessage;
import com.dodo.blog.ui.message.MessageLevel;
import com.dodo.blog.ui.util.Localization;

import javax.validation.ConstraintViolation;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public abstract class AbstractPage
        extends Page
{
    private static final long serialVersionUID = 1L;

    private TwoColumnLayout layout = new TwoColumnLayout();

    public void add( Component component )
    {
        layout.getSection().add( component );
    }

    @Override
    public String getPageContent()
    {
        return layout.render().toString();
    }

    public void setHeader( String header )
    {
        layout.getH1().setInnerHtml( header );
        layout.getHead().add( new Title( header ) );
    }

    public String localize( String key, String... params )
    {
        return Localization.localize( key, params );
    }

    public void info( String message )
    {
        log( message, MessageLevel.INFO );
    }

    public void warn( String message )
    {
        log( message, MessageLevel.WARN );
    }

    public void error( BackendException e )
    {
        if ( e instanceof ValidationException )
        {
            for ( ConstraintViolation cv : ( ( ValidationException ) e ).getConstraintViolations() )
            {
                RequestCycle.get().addMessage( new FieldMessage( cv.getPropertyPath().toString(), cv.getMessage(), MessageLevel.ERROR ) );
            }
        }
        else
        {
            error( "error." + e.getClass().getSimpleName() );
        }
    }

    public void error( String message )
    {
        log( message, MessageLevel.ERROR );
    }

    public void log( String message, MessageLevel level )
    {
        if ( AjaxListenerHelper.isAjax() )
        {
            RequestCycle.get().addMessage( new FeedbackMessage( message, level ) );
        }
        else
        {
            Panel feedback = new Panel();
            feedback.add( message );
            feedback.setClassName( "feedback-box" );
            feedback.addClassName( "feedback-box-" + level.name().toLowerCase() );

            layout.getSection().prepend( feedback );
        }
    }

    public String getParameter( String name )
    {
        return getRequest().getParameter( name );
    }

    public Long getLongParameter( String name )
    {
        return getParameter( name ) != null ? Long.valueOf( getParameter( name ) ) : null;
    }

    public Account getLoggedInAccount()
    {
        return WebApplication.get().getLoggedInAccount();
    }

    public void addAside( Component component )
    {
        layout.getAside().add( component );
    }
}
